

import sys
import time
import traceback
import threading


from PyQt4.QtGui import *
from PyQt4.QtCore import QSettings, QVariant, QSize, QPoint, QByteArray, qDebug


from dexen.system.client import ui
from dexen.system.client import db
from dexen.system.common import service
from dexen.system.common.states import JobState
from dexen.system.client.proxy import ServerProxy, JobProxy
from dexen.system.client.ui.job_main import JobWidget
from dexen.system.client.ui.main_widget import MainWidget
from dexen.system.client.ui.conn_widget import ConnectionWidget


# globally used by all the modules in gui
svr_proxy = ui.svr_proxy

APP_TITLE = "DEXEN-CLIENT"


class MainWindow(QMainWindow):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.mainWidget = MainWidget()
        self.setCentralWidget(self.mainWidget)
        self.setGeometry(200, 200, 800, 600)
        self.setWindowTitle(APP_TITLE)
        #self._create_actions()
        #self._create_menus()
        #self._create_tool_bars()
        
        # Restore user settings
        settings = QSettings()
        size = settings.value("MainWindow/Size",
                              QVariant(QSize(600, 400))).toSize()
        self.resize(size)
        pos = settings.value("MainWindow/Position",
                             QVariant(QPoint(200, 200))).toPoint()
        self.move(pos)
        self.restoreState(settings.value("MainWindow/State").toByteArray())
        
        
    def _create_actions(self):
        self.snj_act = QAction("Start a new job", self)

    def _create_menus(self):
        file_menu = self.menuBar().addMenu(QString("&File"))
        file_menu.addAction(self.snj_act)
    
    def _create_tool_bars(self):
        self.toolbar = QToolBar(self) 
        self.addToolBar(self.toolbar)
        self.toolbar.addAction(self.snj_act)
        self.toolbar.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Fixed)
        self.toolbar.addSeparator()
        self.toolbar.addWidget(self.conn_widget)
        
    def show(self):
        QMainWindow.show(self)

    def get_main_widget(self):
        return self.mainWidget
    
    def closeEvent(self, event):
        qDebug("MainWindow:closeEvent")
        from dexen.system.client import settings_db
        settings_db.close()
        
        # Save user settings
        settings = QSettings()
        settings.setValue("MainWindow/Size", self.size())
        settings.setValue("MainWindow/Position", self.pos())
        settings.setValue("MainWindow/State", self.saveState())
        
        self.mainWidget.save_state()
        #event.accept()
        

#===============================================================================
# Main
#===============================================================================
def main():
    db.initialize()
    app = QApplication(sys.argv)
    app.setOrganizationName("Nus")
    app.setApplicationName("Dexen")
    window = MainWindow()
    window.show()    
    sys.exit(app.exec_())

if __name__ == "__main__":
    main()

